package com.eshealthy.serviceImpl;

import com.eshealthy.dao.AdminDao;
import com.eshealthy.entity.*;
import com.eshealthy.service.AdminService;
import com.eshealthy.utils.EsDevice;
import com.eshealthy.utils.EsDrug;
import com.eshealthy.utils.EsLibrary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.io.IOException;
import java.util.List;
@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao adminDao;

//  医生列表
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Doctor> findAllDoctor(Integer page,Integer rows) {
        int start=(page-1)*rows;
        return adminDao.findAllDoctor(start,rows);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Patient> findAllPatient(Integer page,Integer rows) {
        int start=(page-1)*rows;
        return adminDao.findAllPatient(start,rows);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Department> findAllDepartment(Integer page, Integer rows) {
        int start=(page-1)*rows;
        return adminDao.findAllDepartment(start,rows);
    }

    @Override
    public Manager login(Manager manager) {
        Manager managerDB=adminDao.findByTel(manager.getTel());
        if(!ObjectUtils.isEmpty(managerDB)){
            if(managerDB.getPassword().equals(manager.getPassword())){
                return managerDB;
            }else{
                throw new RuntimeException("用户密码输入不正确");
            }
        }else{
            throw new RuntimeException("用户输入错误");
        }

    }

    @Override
    public Integer findTotalsDoctor() {
        return adminDao.findTotalsDoctor();
    }

    @Override
    public Integer findTotalsPatient() {
        return adminDao.findTotalsPatient();
    }

    @Override
    public void deleteDoctor(String id) {
        adminDao.deleteDoctor(id);
    }

    @Override
    public void deletePatient(String id) {
        adminDao.deletePatient(id);
    }

    @Override
    public void saveDoctor(Doctor doctor) {
            adminDao.saveDoctor(doctor);
    }

    @Override
    public Doctor findDoctor(String id) {

        return adminDao.findDoctor(id);
    }

    @Override
    public void savePatient(Patient patient) {
        adminDao.savePatient(patient);
    }

    @Override
    public Patient findPatient(String id) {
        return adminDao.findPatient(id);
    }

    @Override
    public void saveDepartment(Department department) {
        adminDao.saveDepartment(department);
    }

    @Override
    public Department findDepartment(String id) {
        return adminDao.findDepartment(id);
    }

    @Override
    public void deleteDepartment(String id) {
    adminDao.deleteDepartment(id);
    }

    @Override
    public void registerDepartment(Department department) {
        adminDao.registerDepartment(department);
    }

    @Override
    public Integer findTotalsDepartment() {
        return adminDao.findTotalsDepartment();
    }

    @Override
    public void deleteDoctorArea(String id) {
        adminDao.deleteDoctorArea(id);
    }

    @Override
    public List<Drug> findAllDrug(Integer page, Integer rows) throws IOException {
        EsDrug esDrug=new EsDrug();
        return esDrug.drugList(page,rows);
    }

    @Override
    public long findTotalsDrug() throws IOException {
        EsDrug esDrug=new EsDrug();
        return esDrug.count();
    }

    @Override
    public void registerDrug(Drug drug)throws IOException {
        EsDrug esDrug=new EsDrug();
        esDrug.addDrug(drug.getName(), drug.getTelspecification(), drug.getDirections(), drug.getUseage(), drug.getStorage());
    }

    @Override
    public List<MedicalDevice> findAllDevice(Integer page, Integer rows) throws IOException {
        EsDevice esDevice=new EsDevice();
        return esDevice.medicalDeviceList(page, rows);
    }

    @Override
    public long findTotalsDevice() throws IOException {
        EsDevice esDevice=new EsDevice();
        return esDevice.count();
    }

    @Override
    public void registerDevice(MedicalDevice device) throws IOException {
        EsDevice esDevice=new EsDevice();
        esDevice.addDevice(device.getName(), device.getTelspecification(), device.getDirections(), device.getUseage());
    }

    @Override
    public void register(Manager manager) {
        Manager managerDB=adminDao.findAllAdmin(manager.getUsername());
        if(managerDB==null){
            try{
                adminDao.save(manager);
            }catch(Exception e){
                throw new RuntimeException("输入错误");
            }
        }else{
            throw new RuntimeException("用户已存在");
        }

    }

    @Override
    public void registerArea(Integer area) {
    adminDao.registerArea(area);
    }

    @Override
    public void registerLibrary(Library library) throws IOException {
        EsLibrary esLibrary=new EsLibrary();
        esLibrary.addLibrary(library);
    }
}
